<template>
	<view class="page">
		<!-- 搜索框 -->
		<view class="search-box">
			<view class="search">
				<input type="text" v-model="search" placeholder="输入关键词搜索">
			</view>
		</view>
		
		<!-- 导航栏 -->
		<view class="head-tabs">
			<view class="head-box flex align-center justify-between">
				<view :class="['item-tab', tabsIndex == index ? 'item-tab-active':'']" v-for="(item, index) in tabsArr" :key="item.id" @click="selectTab(index)">
					<text>{{ item.name }}</text>
					<view v-if="tabsIndex == index" class="btm-border"></view>
				</view>
			</view>
		</view>
		
		
		<!-- 内容 -->
		<view class="content-box mt-2">
			<view class="item-content mb-2" v-for="(item, index) in list" :key="item.id" v-if="list.length != 0">
				<view class="top-content flex py-1" :class="tabsIndex == 1?'border-btm py-2':''">
					<view class="left-detail" style="width: 100%;">
						<view class="label-name-box flex align-center mb-2">
							<text class="label-name">编号：</text>
							<text>{{ item.orderid }}</text>
						</view>
						<view class="label-address-box flex align-center mb-2">
							<text class="label-name">地址：</text>
							<text>{{ item.address + item.village }}</text>
						</view>
						<view class="label-jl-box flex align-center">
							<text class="label-name">距离您：</text>
							<text>{{ item.distance }}km</text>
						</view>
					</view>
					<view class="right-icon flex-shrink">
						<image @click="openMapAddress(item)" src="/static/images/dw.png" mode=""></image>
					</view>
				</view>
				
				<view class="center-content py-2" v-if="tabsIndex == 1">
					<view class="label-address-box flex align-center mb-2">
						<text class="label-name">端口号：</text>
						<text>{{ item.device_number }}</text>
					</view>
					<view class="label-address-box flex align-center mb-2">
						<text class="label-name">经纬度：</text>
						<text>经度{{ item.longitude }} 纬度{{ item.latitude }}</text>
					</view>
					<view class="label-address-box flex align-center mb-2">
						<text class="label-name">备注：</text>
						<text>{{ item.info }}</text>
					</view>
					<view class="label-address-box flex mb-2">
						<text class="label-name flex-shrink">拍照：</text>
						<view class="images-box flex flex-wrap">
							<image class="mr-2 mb-2" v-for="(imgItem,imgIndex) in item.images" @click="lookImg(index, imgIndex)" :key="index" :src="imgItem" mode="aspectFit"></image>
						</view>
					</view>
				</view>
				
				
				<view class="btm-content mt-3" v-if="tabsIndex != 2">
					<view class="sub-btn" v-show="tabsIndex == 0" @click="installItem(item.id, index)">抢单</view>
					<view class="sub-btn" v-show="tabsIndex == 1" @click="goToPower(item)">维修</view>
					<view class="desc-name" v-show="tabsIndex == 1">提示：抢单锁定2小时，维修后请填写维修信息</view>
				</view>
			</view>
			
			<view class="flex align-center justify-center mt-5" v-else>暂无数据</view>
		</view>
	</view>
</template>

<script>
	import { postMaintainList, postMaintain } from "../../api/user.js";
	export default {
		data() {
			return {
				tabsArr: [{id: 0, name: '待抢单'}, {id: 1, name: '待维修'}, {id: 2, name: '已维修'}],
				tabsIndex: 0,
				
				list: [],
				total: 0,
				
				search: '',
				type: '',
				page: 1,
				limit: 10,
				longitude: '',
				latitude: ''
			}
		},
		onLoad() {
			uni.showLoading()
			
			// #ifdef H5
			this.init()
			// #endif
		},
		
		onShow() {
			
			
			if(this.longitude && this.latitude) {
				this.page = 1;
				this.list = [];
				this.total = 0;
				this.initFunciton();
			}
		},
		
		watch: {
			search(newValue) {
				this.refresh();
			}
		},
		
		methods: {
			init(){
				this.getUserlocation()
			},
			refresh() {
				this.total = 0;
				this.page = 1;
				this.list = [];	
				this.initFunciton()
			},
			
			// 获取经纬度
			getUserlocation() {
				const that = this;
				uni.getLocation({
					type: 'gcj02',
					altitude: true,
					isHighAccuracy: true,
					success(res) {
						that.latitude = res.latitude;
						that.longitude = res.longitude
						that.initFunciton();
					}
				})
			},
			
			async initFunciton() {
				let {search, page, limit, longitude, latitude} = this;
				let data = {search: String(search), page, limit, longitude, latitude};
				data.type = this.tabsArr[this.tabsIndex].id;
				data.token = uni.getStorageSync('token');
				
				let res = await postMaintainList(data);
				uni.hideLoading()
				
				if(res.data.code == 1) {
					let { count, list } = res.data.data;
					this.list.push(...list)
					this.total = count
				}
			},
			
			//切换tab
			selectTab(index) {
				if(index == this.tabsIndex) return
				this.tabsIndex = index;
				this.list = [];
				this.page = 1;
				this.total = 0;
				this.initFunciton();
			},
			
			//点击定位导航
			openMapAddress(item) {
				let { latitude, longitude, village, address} = item;
				this.$wx.openLocation({
					latitude,
					longitude,
					address,
					name: village,
				})
			},
			
			
			//点击抢单
			async installItem(id, index) {
				let data = { id };
				data.token = uni.getStorageSync('token');
				
				let res = await postMaintain(data);
				if(res.data.code == 1) {
					uni.showToast({
						title: "抢单成功",
						icon: 'success'
					})
					this.refresh()
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			
			//安装抢单
			goToPower(item) {
				let { latitude, longitude, device_number, id } = item;
				uni.navigateTo({
					url: `/pages/user/repairUpFrom?latitude=${latitude}&longitude=${longitude}&device_number=${device_number}&id=${id}`
				})
			},
			
			//预览图片
			lookImg(index, imgIndex) {
				let { images } = this.list[index];
				uni.previewImage({
					urls: images,
					current: imgIndex
				})
			}
		},
		
		onReachBottom() {
			uni.showLoading();
			if(this.list.length < this.total) {
				++this.page;
				this.initFunciton()
			} else {
				uni.hideLoading();
				uni.showToast({
					title: '没有更多了',
					icon: 'none'
				})
			}
		}
	}
</script>

<style scoped>
.search-box {
	padding: 30rpx 30rpx 0;
}
.search {
	width: 100%;
	height: 72rpx;
	border-radius: 150rpx;
	background-color: white;
	padding: 0 35rpx;
	box-sizing: border-box;
}
.search input {
	width: 100%;
	height: 100%;
	font-size: 26rpx;
}
.head-tabs {
	width: 100%;
	box-sizing: border-box;
	padding: 0 70rpx;
}
.head-box {
	width: 100%;
	height: 100%;
}
.item-tab {
	padding: 26rpx 0;
	font-size: 32rpx;
	color: #333333;
	font-weight: 500;
	position: relative;
}
.head-box .item-tab-active {
	font-weight: bold !important;
	color: #49C265 !important;
}
.btm-border {
	width: 36rpx;
	height: 8rpx;
	background: #49C265;
	border-radius: 4rpx;
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
}
.item-content {
	background: white;
	padding: 20rpx 30rpx;
}
.label-name-box {
	font-size: 32rpx;
	color: #000000;
	font-weight: bold;
}
.label-address-box {
	font-size: 30rpx;
	color: #333333;
	font-weight: 500;
}
.label-jl-box {
	font-size: 30rpx;
	color: #0B8B28;
	font-weight: 500;
}
.sub-btn {
	width: 100%;
	height: 70rpx;
	background-color: #49C164;
	border-radius: 10rpx;
	font-size: 26rpx;
	font-weight: 500;
	color: white;
	text-align: center;
	line-height: 70rpx;
}
.right-icon {
	padding: 0 90rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-left: solid 1rpx #EFEFEF;
}
.right-icon image {
	width: 39rpx;
	height: 51rpx;
}
.desc-name {
	font-size: 26rpx;
	color: #999999;
	font-weight: 500;
	line-height: 26rpx;
	text-align: center;
	margin-top: 25rpx;
}
.border-btm {
	border-bottom: dashed 1rpx rgb(245,245,245);
}
.images-box image{
	width: 260rpx;
	height: 100rpx;
}
</style>
